package StudyCode.summary.Basic.numberformat;

import java.util.Formattable;
import java.util.FormattableFlags;
import java.util.Formatter;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CustomFormatter {
	public static void main(String[] argv) {
		MyFormattable fp = new MyFormattable("Hello", "World");
		System.out.printf("%s %n", fp);
		System.out.printf("%#s %n", fp);
		System.out.printf("%S %n", fp);
		System.out.printf("%#S %n", fp);
	}
}

class MyFormattable implements Formattable {
	private String src1 = "";
	private String src2 = "";

	public MyFormattable(String src1, String src2) {
		this.src1 = src1;
		this.src2 = src2;
	}

	@Override
	public void formatTo(Formatter formatter, int flags, int width, int precision) {
		String str = this.src1 + " " + this.src2;

		int alternateFlagValue = FormattableFlags.ALTERNATE & flags;
		if (alternateFlagValue == FormattableFlags.ALTERNATE) {
			str = this.src2 + ", " + this.src1;
		}

		int upperFlagValue = FormattableFlags.UPPERCASE & flags;
		if (upperFlagValue == FormattableFlags.UPPERCASE) {
			str = str.toUpperCase();
		}

		formatter.format(str);
	}
}
